//
//  AppleTouchIDManager.m
//  ClassManager
//
//  Created by mac on 2018/1/13.
//  Copyright © 2018年 GZX. All rights reserved.
//

#import "AppleTouchIDManager.h"
#import <LocalAuthentication/LocalAuthentication.h>

@implementation AppleTouchIDManager


-(BOOL)CheckCanSupportTouchID{
    LAContext *context = [[LAContext alloc] init]; // 初始化上下文对象
    NSError *error = nil;
    // 判断设备是否支持指纹识别功能
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
        return YES;
    }
    else{
        return NO;
    }
}
-(BOOL)CheckCanOpenTouchID{
    return [[[NSUserDefaults standardUserDefaults]objectForKey:@"startAutoLoginState"] boolValue];
}
-(void)OpenTouchIDWith:(BOOL)Open{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:Open] forKey:@"startAutoLoginState"];
}

-(void)StratCheckTouchID:(void(^)(BOOL success,int code))reply{
    LAContext *myContext = [[LAContext alloc] init];
    // 这个属性是设置指纹输入失败之后的弹出框的选项
    myContext.localizedFallbackTitle = @"忘记密码";
    NSString *myLocalizedReasonString = @"请按住Home键完成验证";
    if ([self CheckCanSupportTouchID]) {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {
            int errcode = 0;
            if (!success) {
                errcode =(int)error.code;
            }
            //回调主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                reply(success,errcode);
            });
        }];
    }
    else{
        reply(NO,999);
    }
}



@end
